package com.firefly.service.query.mc;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.firefly.dao.dao.mc.po.McMenuPo;
import com.firefly.dao.dao.mc.po.McRolePermissionPo;
import com.firefly.dao.dao.mc.po.McRolePo;
import com.firefly.dao.dao.mc.service.McMenuService;
import com.firefly.dao.dao.mc.service.McRolePermissionService;
import com.firefly.dao.dao.mc.service.McRoleService;
import com.firefly.dao.assembler.mc.McRoleAssembler;
import com.firefly.dao.dto.mc.McRoleQueryDto;
import com.firefly.dao.vo.CommonListVo;
import com.firefly.dao.vo.mc.McRolePermissionVo;
import com.firefly.dao.vo.mc.McRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2023/1/31
 **/
@Service
public class McRoleQuery {
    private final McRoleService roleService;
    private final McRolePermissionService rolePermissionService;
    private final McMenuService menuService;
    private final McRoleAssembler roleAssembler;
    @Autowired
    public McRoleQuery(McRoleService roleService, McRolePermissionService rolePermissionService, McMenuService menuService, McRoleAssembler roleAssembler){
        this.roleService = roleService;
        this.rolePermissionService = rolePermissionService;
        this.menuService = menuService;
        this.roleAssembler = roleAssembler;
    }
    // 获取列表
    public CommonListVo getList(McRoleQueryDto request) {
        QueryWrapper<McRolePo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!request.key.isEmpty(), "name", request.key);
        Long count = roleService.count(queryWrapper);
        queryWrapper.last("limit " + request.offset + "," + request.pageSize);
        List<McRolePo> all = roleService.list(queryWrapper);
        CommonListVo result = new CommonListVo();
        result.setCount(count);
        result.setList(roleAssembler.getListVo(all));
        return result;
    }

    public McRoleVo getDetail(int id){
        McRolePo detail = roleService.getById(id);
        return roleAssembler.getVo(detail);
    }

    // 获取权限信息
    public List<McRolePermissionVo> getPermission(McRoleQueryDto request){
        // 先检查角色权限
        QueryWrapper<McRolePermissionPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", request.detailId).select("id", "menu_id");
        List<McRolePermissionPo> rp = rolePermissionService.list(queryWrapper);
        List<McMenuPo> all = menuService.list(null);
        // 转格式
        List<McRolePermissionVo> treeMenu = createTree(rp, all, 0);
        return treeMenu;
    }

    // 递归树形菜单
    private List<McRolePermissionVo> createTree(List<McRolePermissionPo> privs, List<McMenuPo> menus, Integer pid) {
        List<McRolePermissionVo> treeMenu = new ArrayList<>();
        for (McMenuPo menu : menus) {
            if (menu.getParentId().equals(pid)) {
                McRolePermissionVo tmp = new McRolePermissionVo();
                tmp.setMenuId(menu.getId());
                tmp.setTitle(menu.getName());
                tmp.setExpand(false);
                tmp.setChecked(false);
                // 所有子菜单都选中的时候，此菜单选中checked，部分选中时selected
                List<Object> child_count = new ArrayList<>();
                child_count = menus.stream().filter((McMenuPo m) -> Arrays.asList(m.getParentId()).contains(menu.getId())).collect(Collectors.toList());
                // 没有子菜单的时候，判断他本身
                if (child_count.size() == 0) {
                    child_count = privs.stream().filter((McRolePermissionPo rpt) -> Arrays.asList(rpt.getMenuId()).contains(menu.getId())).collect(Collectors.toList());
                    boolean checked = child_count.size() > 0 ? true : false;
                    tmp.setChecked(checked);
                }
                List<McRolePermissionVo> children = createTree(privs, menus, tmp.getMenuId());
                tmp.setChildren(children);
                treeMenu.add(tmp);
            }
        }
        return treeMenu;
    }
}
